package com.example.administrator.android2019.ch3Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.android2019.R;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.sql.BatchUpdateException;

public class Ch3FileInnerActivity extends AppCompatActivity {
    private Button btn1,btn2;
    private TextView tv;
    String fileName="hello txt";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ch3_file_inner);
        btn1=findViewById(R.id.ch3_file_inner_btn_read);
        btn2=findViewById(R.id.ch3_file_inner_btn_write);
        tv=findViewById(R.id.ch3_file_inner_tv);

        View.OnClickListener click=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case 1:
                        read();
                        break;
                    case 2:
                        write();
                        break;
                }
            }
        };
        btn1.setOnClickListener(click);
        btn2.setOnClickListener(click);
    }
    private  void read() {
        FileInputStream in=null;
        BufferedReader br=null;
        try {
            in = this.openFileInput(fileName);
            br = new BufferedReader(new InputStreamReader(in));
            String line = null;
            String s = null;
            while ((line = br.readLine()) != null) {
                s = s + line + "\n";
            }
            tv.setText(s);
        } catch (Exception e) {
            e.printStackTrace();
        }

            finally {
            if (in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        }


    private  void write(){
        String message="hello\n中国";
        FileOutputStream out=null;
        BufferedWriter bw=null;
        try {
            out=this.openFileOutput(fileName,MODE_PRIVATE);
            bw=new BufferedWriter(new OutputStreamWriter(out));
            bw.write(message);
            bw.flush();
            Toast.makeText(this,"写入成功",Toast.LENGTH_SHORT).show();;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
